<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="400" destroy-on-close @closed="$emit('closed')">
		<el-form ref="dialogForm" :model="form" :rules="rules" label-width="100px" label-position="left">
			<el-form-item label="排序号">
				<el-input-number v-model="form.sortNo" placeholder="=排序号"></el-input-number>
			</el-form-item>
			<el-form-item label="接口编号" prop="apiCode">
				<el-input v-model="form.apiCode" placeholder="接口编号" maxlength="40"></el-input>
			</el-form-item>
			<el-form-item label="接口名称" prop="title">
				<el-input v-model="form.title" placeholder="接口名称" maxlength="40"></el-input>
			</el-form-item>
			<el-form-item label="接口地址" prop="apiUrl">
				<el-input v-model="form.apiUrl" placeholder="接口地址" maxlength="180"></el-input>
			</el-form-item>
			<el-form-item label="接口参数" prop="params">
				<el-input v-model="form.params" placeholder="接口参数" maxlength="180"></el-input>
			</el-form-item>
			<el-form-item label="接口类型" prop="method">
				<el-radio-group v-model="form.method">
					<el-radio-button label="get">GET</el-radio-button>
					<el-radio-button label="post">POST</el-radio-button>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="是否启用" prop="status">
				<el-radio-group v-model="form.status">
					<el-radio-button label="1">启用</el-radio-button>
					<el-radio-button label="0">停用</el-radio-button>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="备注">
				<el-input v-model="form.remark" maxlength="150" placeholder="备注" type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button type="primary" :loading="isSaveing" @click="submit">保存</el-button>
			<el-button @click="visible=false">取消</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			loading: false,
			mode: "add",
			titleMap: {
				add: '新增接口',
				edit: '编辑接口'
			},
			form: {
				apiId: "",
				title:'',
				sortNo: 0,
				apiCode: '',
				apiUrl: '',
				method: 'get',
				params:'',
				status: '1',
				remark: ''
			},
			visible: false,
			isSaveing: false,
			rules: {
				apiCode: [
					{required: true, message: '数据源名称不能为空', trigger: 'blur'}
				],
				title: [
					{required: true, message: '接口名称不能为空', trigger: 'blur'}
				],
				apiUrl: [
					{required: true, message: '接口地址不能为空', trigger: 'blur'}
				],
				dbPassWord: [
					{required: true, message: '数据库密码不能为空', trigger: 'blur'}
				]
			},
		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.bigdata.bigDataApi.insertBigDataApi.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.bigdata.bigDataApi.updateBigDataApi.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.apiId = data.apiId;
			this.form.title = data.title;
			this.form.sortNo = data.sortNo;
			this.form.apiCode = data.apiCode;
			this.form.apiUrl = data.apiUrl;
			this.form.method = data.method;
			this.form.params = data.params;
			this.form.status = data.status;
			this.form.remark = data.remark
		}
	}
}
</script>

<style>
</style>
